package com.example.coursemanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.mapper.CampusCommentsMapper;
import com.example.coursemanagement.mapper.CampusMapper;
import com.example.coursemanagement.pojo.Campus;
import com.example.coursemanagement.pojo.Campus_comments;
import com.example.coursemanagement.service.ICampusCommentsService;
import com.example.coursemanagement.service.ICampusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 这是目录表格，通过这个查寻课程的各个章节链接 服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-10-27
 */
@Service
public class CampusCommentsService extends ServiceImpl<CampusCommentsMapper, Campus_comments> implements ICampusCommentsService {

    @Autowired
    private CampusCommentsMapper commentsMapper;

    @Autowired
    private CampusMapper campusMapper;
    @Override
    public CommonResult<Object> publishComments(Campus_comments campus_comments) {
        if(Objects.isNull(campus_comments.getCommentContext())){
            return CommonResult.failed("内容为空");
        }
        if(commentsMapper.insert(campus_comments)>0){
            return CommonResult.success(campus_comments,"评论成功");
        }
        return CommonResult.failed("评论失败");
    }

    @Override
    public CommonResult<Object> updateComments(Campus_comments campus_comments) {
        if(commentsMapper.updateById(campus_comments)>0)
            return CommonResult.success(campus_comments,"更新成功");
        return CommonResult.failed("更新失败");
    }

    @Override
    public CommonResult<Object> deleteCommentsById(Integer commentId) {
        if(commentsMapper.deleteById(commentId)>0)
            return CommonResult.success("删除成功");
        return CommonResult.failed("删除失败");
    }

    @Override
    public CommonResult<Object> selectCommentsByUserId(Integer userId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",userId);
        List<Campus_comments> list = commentsMapper.selectByMap(map);
        if(list.size()==0){
            return CommonResult.success(null,"该用户暂未评论");
        }
        return CommonResult.success(list,"查询成功，共有"+list.size()+"条");
    }

    @Override
    public CommonResult<Object> selectCommentsByCampusId(Integer campusId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("campus_id",campusId);
        Campus campus = campusMapper.selectById(campusId);
        if(Objects.isNull(campus)){
            return CommonResult.failed("该资讯不存在");
        }
        List<Campus_comments> list = commentsMapper.selectByMap(map);
        if(list.size()==0){
            return CommonResult.success(null,"该资讯暂无评论");
        }
        return CommonResult.success(list,"查询成功，共有"+list.size()+"条");
    }
}
